package writer;

import org.junit.jupiter.api.Test;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @author 王艺锡
 * @version 1.0
 * FileWriter以字符的形式写入文件
 */
public class FileWriter_ {
    public static void main(String[] args) {

    }


    @Test
    public void FileWriter(){
        String filePath = "e:\\note.txt";
        FileWriter fileWriter = null;

        try {
            //1.fileWriter = new FileWriter(filePath)在创建文件时以覆盖的形式写入数据
            //2.fileWriter = new FileWriter(filePath,true)在创建文件的时候以追加的形式写入数据
            fileWriter = new FileWriter(filePath);
            String buf = "风雨之后，定见彩虹！";
            fileWriter.write(buf.toCharArray());
            fileWriter.write(buf);
            fileWriter.write(buf,0,3);
            //fileWriter.write('a');
            //fileWriter.write(buf.toCharArray(),0,5);//写入部分数据
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //FileWriter一定要关闭流(close)或者刷新流(flush)才能真正的将数据写入文件
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
